import { Injectable } from '@angular/core';
import { UserInfo } from 'src/app/model';
import { CoreModule } from '../core.module';
import { StorageService } from '../sotrage/storage.service';
import { TokenService } from '../token';

@Injectable({
  providedIn: CoreModule
})
export class UserInfoService {

  private readonly userInfoKey = 'wenyao-web-fallet-user-info';

  public getUserInfo() {
    return this.storageService.get<UserInfo>(this.userInfoKey);
  }

  public saveUserInfo(userInfo: UserInfo) {
    this.storageService.set(this.userInfoKey, userInfo);
    this.tokenService.saveToken(userInfo.loginId, userInfo.token);
  }

  public clearUserInfo() {
    this.storageService.remove(this.userInfoKey);
    this.tokenService.removeToken();

    this.storageService.clear();
  }

  constructor(
    private tokenService: TokenService,
    private storageService: StorageService
  ) { }
}
